package com.sgu6.sgu6_machine.presenter;


import android.net.Network;
import android.util.Log;

import com.sgu6.middleware.log.Logger;
import com.sgu6.sgu6_machine.dto.ElectricityDto;
import com.sgu6.sgu6_machine.dto.ElectricityParams;
import com.sgu6.sgu6_machine.dto.ListGoodsSimpleDto;
import com.sgu6.sgu6_machine.config.MachineConfig;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.dto.ApiErrorDto;
import com.sgu6.sgu6_machine.dto.AppUpdateDto;
import com.sgu6.sgu6_machine.dto.BannerDto;
import com.sgu6.sgu6_machine.dto.BarnStockParamsDto;
import com.sgu6.sgu6_machine.dto.CreateMemberCardDto;
import com.sgu6.sgu6_machine.dto.CreateMemberCardParams;
import com.sgu6.sgu6_machine.dto.MachineAccountDto;
import com.sgu6.sgu6_machine.dto.MachineLoginDto;
import com.sgu6.sgu6_machine.dto.MachineMakeRecordDto;
import com.sgu6.sgu6_machine.dto.MakeRecordDto;
import com.sgu6.sgu6_machine.dto.MemberCardInfoDto;
import com.sgu6.sgu6_machine.dto.MemberCardPayParamsDto;
import com.sgu6.sgu6_machine.dto.PayDto;
import com.sgu6.sgu6_machine.dto.SaleRulesDto;
import com.sgu6.sgu6_machine.dto.SysNoticeDto;
import com.sgu6.sgu6_machine.dto.UpdateMachinePwdDto;
import com.sgu6.sgu6_machine.event.AppUpdateEvent;
import com.sgu6.sgu6_machine.event.UpdateGoodsInfoEvent;
import com.sgu6.sgu6_machine.network.NetWork;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MachineP {

    public final String TAG = this.getClass().getSimpleName();

    public void getGoodsByMachineId(String machineId) {
        try {
            NetWork.getApiService().getGoodsByMachineId(machineId).enqueue(new Callback<List<ListGoodsSimpleDto>>() {
                @Override
                public void onResponse(Call<List<ListGoodsSimpleDto>> call, Response<List<ListGoodsSimpleDto>> response) {
                    if (response.body() != null) {
                        EventBus.getDefault().post(new UpdateGoodsInfoEvent(response.body()));
                    }
                }

                @Override
                public void onFailure(Call<List<ListGoodsSimpleDto>> call, Throwable t) {
                    Log.i(TAG, "onFailure: ");
                }
            });
        }catch(Exception e){
            Log.i(TAG, "getGoodsByMachineId: ");
        }

    }

    public void getMemberCardInfo(String internalCode) {
        try {
            NetWork.getAdminApiService().getMemberCardInfo(internalCode).enqueue(new Callback<MemberCardInfoDto>() {
                @Override
                public void onResponse(Call<MemberCardInfoDto> call, Response<MemberCardInfoDto> response) {
                    if (response.body() != null) {
                        Log.i(TAG, "onResponse: 获取卡信息成功：" + response.body());
                        EventBus.getDefault().post(response.body());
                    }
                }

                @Override
                public void onFailure(Call<MemberCardInfoDto> call, Throwable t) {
                    Log.i(TAG, "onFailure: 获取卡信息失败");

                }
            });
        }catch (Exception e){
            Log.i(TAG, "getMemberCardInfo: ");
        }
    }

    public void getMemberCardByCardCode(String cardCode) {
        try {
            NetWork.getApiService().getMemberCardByCardCode(cardCode).enqueue(new Callback<MemberCardInfoDto>() {
                @Override
                public void onResponse(Call<MemberCardInfoDto> call, Response<MemberCardInfoDto> response) {
                    if (response.body() != null) {
                        Logger.i(TAG, "onResponse: 获取卡信息成功：" + response.body(), MachineContainer.machineConfigDto.logcat);
                        EventBus.getDefault().post(response.body());
                    }
                }

                @Override
                public void onFailure(Call<MemberCardInfoDto> call, Throwable t) {
                    Logger.i(TAG, "onFailure: 获取卡信息失败", MachineContainer.machineConfigDto.logcat);

                }
            });
        }catch (Exception e){
            Log.i(TAG, "getMemberCardByCardCode: ");
        }

    }

    public void payByMemberCard(MemberCardPayParamsDto memberCardPayParamsDto) {
        try {
            NetWork.getApiService().payByMemberCard(memberCardPayParamsDto).enqueue(new Callback<ApiErrorDto>() {
                @Override
                public void onResponse(Call<ApiErrorDto> call, Response<ApiErrorDto> response) {
                    if (response.body() != null) {
                        EventBus.getDefault().post(response.body());
                    }
                }

                @Override
                public void onFailure(Call<ApiErrorDto> call, Throwable t) {
                    Log.i(TAG, "onFailure: ");
                }
            });
        }catch (Exception e){
            Log.i(TAG, "getMemberCardByCardCode: ");
        }
    }

    public void memberCardWaterPay(MemberCardPayParamsDto memberCardPayParamsDto) {
        try {
            NetWork.getApiService().memberCardWaterPay(memberCardPayParamsDto).enqueue(new Callback<ApiErrorDto>() {
                @Override
                public void onResponse(Call<ApiErrorDto> call, Response<ApiErrorDto> response) {
                    if (response.body() != null) {
                        EventBus.getDefault().post(response.body());
                    }
                }

                @Override
                public void onFailure(Call<ApiErrorDto> call, Throwable t) {
                    Log.i(TAG, "onFailure: ");
                }
            });
        }catch (Exception e){
            Log.i(TAG, "getMemberCardByCardCode: ");
        }
    }

    public void alterBarnStock(BarnStockParamsDto barnStockParamsDto) {
        try {
            NetWork.getApiService().alterBarnStock(barnStockParamsDto).enqueue(new Callback<ApiErrorDto>() {
                @Override
                public void onResponse(Call<ApiErrorDto> call, Response<ApiErrorDto> response) {

                }

                @Override
                public void onFailure(Call<ApiErrorDto> call, Throwable t) {

                }
            });
        }catch (Exception e){
            Log.i(TAG, "getMemberCardByCardCode: ");
        }

    }


    public void login(String machineNo, String password) {
        try {
            NetWork.getAdminApiService().login(new MachineAccountDto(machineNo, password)).enqueue(new Callback<MachineLoginDto>() {
                @Override
                public void onResponse(Call<MachineLoginDto> call, Response<MachineLoginDto> response) {
                    Log.i(TAG, "onResponse: " + response.body());
                    if (response.body() == null) {
                        EventBus.getDefault().post(new MachineLoginDto(true));
                    } else {
                        MachineLoginDto dto = new MachineLoginDto(false);
                        dto.setErrCode(response.body().getErrCode());
                        dto.setErrMsg(response.body().getErrMsg());
                        EventBus.getDefault().post(dto);
                    }
                }

                @Override
                public void onFailure(Call<MachineLoginDto> call, Throwable t) {
                    Log.i(TAG, "onFailure: " + t.getLocalizedMessage());
                    EventBus.getDefault().post(new MachineLoginDto(false));
                }
            });
        }catch (Exception e){
            Log.i(TAG, "getMemberCardByCardCode: ");
        }

    }

    public void updatePwd(String machineNo, String password) {
        try {
            List<MachineAccountDto> machines = new ArrayList<>();
            machines.add(new MachineAccountDto(machineNo, password));
            NetWork.getAdminApiService().updatePassword(machines).enqueue(new Callback<UpdateMachinePwdDto>() {
                @Override
                public void onResponse(Call<UpdateMachinePwdDto> call, Response<UpdateMachinePwdDto> response) {
                    if (response.body() == null) {
                        EventBus.getDefault().post(new UpdateMachinePwdDto(true));
                    } else {
                        UpdateMachinePwdDto dto = new UpdateMachinePwdDto(false);
                        dto.setErrCode(response.body().getErrCode());
                        dto.setErrMsg(response.body().getErrMsg());
                        EventBus.getDefault().post(dto);
                    }
                }

                @Override
                public void onFailure(Call<UpdateMachinePwdDto> call, Throwable t) {
                    EventBus.getDefault().post(new UpdateMachinePwdDto(false));
                }
            });
        }catch (Exception e){
            Log.i(TAG, "getMemberCardByCardCode: ");
        }
    }

    public void getLastAppList(int page, int pageSize, int versionType) {
        try {
            NetWork.getAdminApiService().getAppList(page, pageSize, versionType).enqueue(new Callback<AppUpdateDto>() {
                @Override
                public void onResponse(Call<AppUpdateDto> call, Response<AppUpdateDto> response) {
                    if (response.body() != null) {
                        EventBus.getDefault().post(new AppUpdateEvent(response.body()));
                    }
                }

                @Override
                public void onFailure(Call<AppUpdateDto> call, Throwable t) {
                    Log.i(TAG, "onFailure: " + t.getLocalizedMessage());
                }
            });
        }catch (Exception e){
            Log.i(TAG, "getMemberCardByCardCode: ");
        }
    }

    public void postBackMakeRecord(MakeRecordDto dto) {
        try {
            NetWork.getApiService().postBackMakeRecord(dto).enqueue(new Callback<MachineMakeRecordDto>() {
                @Override
                public void onResponse(Call<MachineMakeRecordDto> call, Response<MachineMakeRecordDto> response) {

                }

                @Override
                public void onFailure(Call<MachineMakeRecordDto> call, Throwable t) {

                }
            });
        }catch (Exception e){
            Log.i(TAG, "getMemberCardByCardCode: ");
        }
    }

    public void getSysNoticeByType(int type) {
        try {
            NetWork.getAdminApiService().getSysNoticeByType(type).enqueue(new Callback<SysNoticeDto>() {
                @Override
                public void onResponse(Call<SysNoticeDto> call, Response<SysNoticeDto> response) {
                    if (response.body() != null) {
                        Log.i(TAG, "onResponse: 获取系统通知成功");
                        EventBus.getDefault().post(response.body());
                    }
                }

                @Override
                public void onFailure(Call<SysNoticeDto> call, Throwable t) {
                    Log.i(TAG, "onFailure: 获取系统通知失败");
                }
            });
        }catch (Exception e){
            Log.i(TAG, "getMemberCardByCardCode: ");
        }
    }


    public void getSaleRules(int typeId, String merchantId) {
        try {
            NetWork.getAdminApiService().getRechargeRules(typeId, merchantId).enqueue(new Callback<SaleRulesDto>() {
                @Override
                public void onResponse(Call<SaleRulesDto> call, Response<SaleRulesDto> response) {
                    if (response.body() != null) {
                        Log.i(TAG, "onResponse: 获取系统通知成功");
                        EventBus.getDefault().post(response.body());
                    }
                }

                @Override
                public void onFailure(Call<SaleRulesDto> call, Throwable t) {

                }
            });
        }catch (Exception e){
            Log.i(TAG, "getSaleRules: ");
        }

    }


    public void createMemberCard(CreateMemberCardParams params) {
        try {
            NetWork.getAdminApiService().createMemberCard(params).enqueue(new Callback<CreateMemberCardDto>() {
                @Override
                public void onResponse(Call<CreateMemberCardDto> call, Response<CreateMemberCardDto> response) {
                    if (response.body() == null) {
                        EventBus.getDefault().post(new CreateMemberCardDto(true));
                    } else {
                        CreateMemberCardDto dto = new CreateMemberCardDto(false);
                        dto.setErrCode(response.body().getErrCode());
                        dto.setErrMsg(response.body().getErrMsg());
                        EventBus.getDefault().post(dto);
                    }
                }

                @Override
                public void onFailure(Call<CreateMemberCardDto> call, Throwable t) {
                    EventBus.getDefault().post(new CreateMemberCardDto(false));
                }
            });
        }catch (Exception e){
            Log.i(TAG, "createMemberCard: ");
        }

    }


    public void postElectricity(ElectricityParams params) {

        NetWork.getApiService().postElectricity(params).enqueue(new Callback<ElectricityDto>() {
            @Override
            public void onResponse(Call<ElectricityDto> call, Response<ElectricityDto> response) {
                if (response.body() == null) {
                    Log.i(TAG, "onResponse: 1");
                } else {
                    Log.i(TAG, "onResponse: 2");
                }
            }

            @Override
            public void onFailure(Call<ElectricityDto> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t.getLocalizedMessage());
            }
        });
    }

    public void getPayUrl(String params){
        NetWork.getApiService().getPayUrl(params).enqueue(new Callback<PayDto>() {
            @Override
            public void onResponse(Call<PayDto> call, Response<PayDto> response) {
                if (response.body() != null) {
                    EventBus.getDefault().post(response.body());
                }
            }

            @Override
            public void onFailure(Call<PayDto> call, Throwable t) {
                Log.i(TAG, "onFailure: ");
            }
        });
    }




}
